home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / DMAKE38B.ARJ / TESTMEM.C < prev    next >
C/C++ Source or Header  |  1992-01-23  |  20KB  |  647 lines

  1. /* 
  2.  * This stuff is all stolen (with permission, since it was in the public
  3.  * domain) from Henry Spencer's string and memory library.  Thanks Henry.
  4.  */
  5.  
  6. /*
  7.  * Test program for string(3) routines.
  8.  * 
  9.  * Note that at least one Bell Labs implementation of the string
  10.  * routines flunks a couple of these tests -- the ones which test
  11.  * behavior on "negative" characters.
  12.  */
  13.  
  14. #include <stdio.h>
  15. #include <string.h>
  16.  
  17. char * index();
  18. char * rindex();
  19.  
  20. #define    STREQ(a, b)    (strcmp((a), (b)) == 0)
  21.  
  22. char *it = "<UNSET>";        /* Routine name for message routines. */
  23. int waserror = 0;        /* For exit status. */
  24.  
  25. char uctest[] = "\004\203";    /* For testing signedness of chars. */
  26. int charsigned;            /* Result. */
  27.  
  28. /*
  29.  - check - complain if condition is not true
  30.  */
  31. void
  32. check(thing, number)
  33. int thing;
  34. int number;            /* Test number for error message. */
  35. {
  36.     if (!thing) {
  37.         printf("%s flunked test %d\n", it, number);
  38.         waserror = 1;
  39.     }
  40. }
  41.  
  42. /*
  43.  - equal - complain if first two args don't strcmp as equal
  44.  */
  45. void
  46. equal(a, b, number)
  47. char *a;
  48. char *b;
  49. int number;            /* Test number for error message. */
  50. {
  51.     check(a != NULL && b != NULL && STREQ(a, b), number);
  52. }
  53.  
  54. char one[50];
  55. char two[50];
  56.  
  57. #ifdef UNIXERR
  58. #define ERR 1
  59. #endif
  60. #ifdef BERKERR
  61. #define ERR 1
  62. #endif
  63. #ifdef ERR
  64. int f;
  65. extern char *sys_errlist[];
  66. extern int sys_nerr;
  67. extern int errno;
  68. #endif
  69.  
  70. /* ARGSUSED */
  71. main(argc, argv)
  72. int argc;
  73. char *argv[];
  74. {
  75.     /*
  76.      * First, establish whether chars are signed.
  77.      */
  78.     if (uctest[0] < uctest[1])
  79.         charsigned = 0;
  80.     else
  81.         charsigned = 1;
  82.  
  83.     /*
  84.      * Then, do the rest of the work.  Split into two functions because
  85.      * some compilers get unhappy about a single immense function.
  86.      */
  87.     first();
  88.     second();
  89.  
  90.     exit((waserror) ? 1 : 0);
  91. }
  92.  
  93. first()
  94. {
  95.     /*
  96.      * Test strcmp first because we use it to test other things.
  97.      */
  98.     it = "strcmp";
  99.     check(strcmp("", "") == 0, 1);        /* Trivial case. */
  100.     check(strcmp("a", "a") == 0, 2);    /* Identity. */
  101.     check(strcmp("abc", "abc") == 0, 3);    /* Multicharacter. */
  102.     check(strcmp("abc", "abcd") < 0, 4);    /* Length mismatches. */
  103.     check(strcmp("abcd", "abc") > 0, 5);
  104.     check(strcmp("abcd", "abce") < 0, 6);    /* Honest miscompares. */
  105.     check(strcmp("abce", "abcd") > 0, 7);
  106.     check(strcmp("a\203", "a") > 0, 8);    /* Tricky if char signed. */
  107.     if (charsigned)                /* Sign-bit comparison. */
  108.         check(strcmp("a\203", "a\003") < 0, 9);
  109.     else
  110.         check(strcmp("a\203", "a\003") > 0, 9);
  111.     check(strcmp("a", "a\203") < 0, 10);    /* Tricky if char signed. */
  112.  
  113.     /*
  114.      * Test strcpy next because we need it to set up other tests.
  115.      */
  116.     it = "strcpy";
  117.     check(strcpy(one, "abcd") == one, 1);    /* Returned value. */
  118.     equal(one, "abcd", 2);            /* Basic test. */
  119.  
  120.     (void) strcpy(one, "x");
  121.     equal(one, "x", 3);            /* Writeover. */
  122.     equal(one+2, "cd", 4);            /* Wrote too much? */
  123.  
  124.     (void) strcpy(two, "hi there");
  125.     (void) strcpy(one, two);
  126.     equal(one, "hi there", 5);        /* Basic test encore. */
  127.     equal(two, "hi there", 6);        /* Stomped on source? */
  128.  
  129.     (void) strcpy(one, "");
  130.     equal(one, "", 7);            /* Boundary condition. */
  131.  
  132.     /*
  133.      * strcat
  134.      */
  135.     it = "strcat";
  136.     (void) strcpy(one, "ijk");
  137.     check(strcat(one, "lmn") == one, 1);    /* Returned value. */
  138.     equal(one, "ijklmn", 2);        /* Basic test. */
  139.  
  140.     (void) strcpy(one, "x");
  141.     (void) strcat(one, "yz");
  142.     equal(one, "xyz", 3);            /* Writeover. */
  143.     equal(one+4, "mn", 4);            /* Wrote too much? */
  144.  
  145.     (void) strcpy(one, "gh");
  146.     (void) strcpy(two, "ef");
  147.     (void) strcat(one, two);
  148.     equal(one, "ghef", 5);            /* Basic test encore. */
  149.     equal(two, "ef", 6);            /* Stomped on source? */
  150.  
  151.     (void) strcpy(one, "");
  152.     (void) strcat(one, "");
  153.     equal(one, "", 7);            /* Boundary conditions. */
  154.     (void) strcpy(one, "ab");
  155.     (void) strcat(one, "");
  156.     equal(one, "ab", 8);
  157.     (void) strcpy(one, "");
  158.     (void) strcat(one, "cd");
  159.     equal(one, "cd", 9);
  160.  
  161.     /*
  162.      * strncat - first test it as strcat, with big counts, then
  163.      * test the count mechanism.
  164.      */
  165.     it = "strncat";
  166.     (void) strcpy(one, "ijk");
  167.     check(strncat(one, "lmn", 99) == one, 1);    /* Returned value. */
  168.     equal(one, "ijklmn", 2);        /* Basic test. */
  169.  
  170.     (void) strcpy(one, "x");
  171.     (void) strncat(one, "yz", 99);
  172.     equal(one, "xyz", 3);            /* Writeover. */
  173.     equal(one+4, "mn", 4);            /* Wrote too much? */
  174.  
  175.     (void) strcpy(one, "gh");
  176.     (void) strcpy(two, "ef");
  177.     (void) strncat(one, two, 99);
  178.     equal(one, "ghef", 5);            /* Basic test encore. */
  179.     equal(two, "ef", 6);            /* Stomped on source? */
  180.  
  181.     (void) strcpy(one, "");
  182.     (void) strncat(one, "", 99);
  183.     equal(one, "", 7);            /* Boundary conditions. */
  184.     (void) strcpy(one, "ab");
  185.     (void) strncat(one, "", 99);
  186.     equal(one, "ab", 8);
  187.     (void) strcpy(one, "");
  188.     (void) strncat(one, "cd", 99);
  189.     equal(one, "cd", 9);
  190.  
  191.     (void) strcpy(one, "ab");
  192.     (void) strncat(one, "cdef", 2);
  193.     equal(one, "abcd", 10);            /* Count-limited. */
  194.  
  195.     (void) strncat(one, "gh", 0);
  196.     equal(one, "abcd", 11);            /* Zero count. */
  197.  
  198.     (void) strncat(one, "gh", 2);
  199.     equal(one, "abcdgh", 12);        /* Count and length equal. */
  200.  
  201.     /*
  202.      * strncmp - first test as strcmp with big counts, then test
  203.      * count code.
  204.      */
  205.     it = "strncmp";
  206.     check(strncmp("", "", 99) == 0, 1);    /* Trivial case. */
  207.     check(strncmp("a", "a", 99) == 0, 2);    /* Identity. */
  208.     check(strncmp("abc", "abc", 99) == 0, 3);    /* Multicharacter. */
  209.     check(strncmp("abc", "abcd", 99) < 0, 4);    /* Length unequal. */
  210.     check(strncmp("abcd", "abc", 99) > 0, 5);
  211.     check(strncmp("abcd", "abce", 99) < 0, 6);    /* Honestly unequal. */
  212.     check(strncmp("abce", "abcd", 99) > 0, 7);
  213.     check(strncmp("a\203", "a", 2) > 0, 8);    /* Tricky if '\203' < 0 */
  214.     if (charsigned)                /* Sign-bit comparison. */
  215.         check(strncmp("a\203", "a\003", 2) < 0, 9);
  216.     else
  217.         check(strncmp("a\203", "a\003", 2) > 0, 9);
  218.     check(strncmp("abce", "abcd", 3) == 0, 10);    /* Count limited. */
  219.     check(strncmp("abce", "abc", 3) == 0, 11);    /* Count == length. */
  220.     check(strncmp("abcd", "abce", 4) < 0, 12);    /* Nudging limit. */
  221.     check(strncmp("abc", "def", 0) == 0, 13);    /* Zero count. */
  222.  
  223.     /*
  224.      * strncpy - testing is a bit different because of odd semantics
  225.      */
  226.     it = "strncpy";
  227.     check(strncpy(one, "abc", 4) == one, 1);    /* Returned value. */
  228.     equal(one, "abc", 2);            /* Did the copy go right? */
  229.  
  230.     (void) strcpy(one, "abcdefgh");
  231.     (void) strncpy(one, "xyz", 2);
  232.     equal(one, "xycdefgh", 3);        /* Copy cut by count. */
  233.  
  234.     (void) strcpy(one, "abcdefgh");
  235.     (void) strncpy(one, "xyz", 3);        /* Copy cut just before NUL. */
  236.     equal(one, "xyzdefgh", 4);
  237.  
  238.     (void) strcpy(one, "abcdefgh");
  239.     (void) strncpy(one, "xyz", 4);        /* Copy just includes NUL. */
  240.     equal(one, "xyz", 5);
  241.     equal(one+4, "efgh", 6);        /* Wrote too much? */
  242.  
  243.     (void) strcpy(one, "abcdefgh");
  244.     (void) strncpy(one, "xyz", 5);        /* Copy includes padding. */
  245.     equal(one, "xyz", 7);
  246.     equal(one+4, "", 8);
  247.     equal(one+5, "fgh", 9);
  248.  
  249.     (void) strcpy(one, "abc");
  250.     (void) strncpy(one, "xyz", 0);        /* Zero-length copy. */
  251.     equal(one, "abc", 10);    
  252.  
  253.     (void) strncpy(one, "", 2);        /* Zero-length source. */
  254.     equal(one, "", 11);
  255.     equal(one+1, "", 12);    
  256.     equal(one+2, "c", 13);
  257.  
  258.     (void) strcpy(one, "hi there");
  259.     (void) strncpy(two, one, 9);
  260.     equal(two, "hi there", 14);        /* Just paranoia. */
  261.     equal(one, "hi there", 15);        /* Stomped on source? */
  262.  
  263.     /*
  264.      * strlen
  265.      */
  266.     it = "strlen";
  267.     check(strlen("") == 0, 1);        /* Empty. */
  268.     check(strlen("a") == 1, 2);        /* Single char. */
  269.     check(strlen("abcd") == 4, 3);        /* Multiple chars. */
  270.  
  271.     /*
  272.      * strchr
  273.      */
  274.     it = "strchr";
  275.     check(strchr("abcd", 'z') == NULL, 1);    /* Not found. */
  276.     (void) strcpy(one, "abcd");
  277.     check(strchr(one, 'c') == one+2, 2);    /* Basic test. */
  278.     check(strchr(one, 'd') == one+3, 3);    /* End of string. */
  279.     check(strchr(one, 'a') == one, 4);    /* Beginning. */
  280.     check(strchr(one, '\0') == one+4, 5);    /* Finding NUL. */
  281.     (void) strcpy(one, "ababa");
  282.     check(strchr(one, 'b') == one+1, 6);    /* Finding first. */
  283.     (void) strcpy(one, "");
  284.     check(strchr(one, 'b') == NULL, 7);    /* Empty string. */
  285.     check(strchr(one, '\0') == one, 8);    /* NUL in empty string. */
  286.  
  287.     /*
  288.      * index - just like strchr
  289.      */
  290.     it = "index";
  291.     check(index("abcd", 'z') == NULL, 1);    /* Not found. */
  292.     (void) strcpy(one, "abcd");
  293.     check(index(one, 'c') == one+2, 2);    /* Basic test. */
  294.     check(index(one, 'd') == one+3, 3);    /* End of string. */
  295.     check(index(one, 'a') == one, 4);    /* Beginning. */
  296.     check(index(one, '\0') == one+4, 5);    /* Finding NUL. */
  297.     (void) strcpy(one, "ababa");
  298.     check(index(one, 'b') == one+1, 6);    /* Finding first. */
  299.     (void) strcpy(one, "");
  300.     check(index(one, 'b') == NULL, 7);    /* Empty string. */
  301.     check(index(one, '\0') == one, 8);    /* NUL in empty string. */
  302.  
  303.     /*
  304.      * strrchr
  305.      */
  306.     it = "strrchr";
  307.     check(strrchr("abcd", 'z') == NULL, 1);    /* Not found. */
  308.     (void) strcpy(one, "abcd");
  309.     check(strrchr(one, 'c') == one+2, 2);    /* Basic test. */
  310.     check(strrchr(one, 'd') == one+3, 3);    /* End of string. */
  311.     check(strrchr(one, 'a') == one, 4);    /* Beginning. */
  312.     check(strrchr(one, '\0') == one+4, 5);    /* Finding NUL. */
  313.     (void) strcpy(one, "ababa");
  314.     check(strrchr(one, 'b') == one+3, 6);    /* Finding last. */
  315.     (void) strcpy(one, "");
  316.     check(strrchr(one, 'b') == NULL, 7);    /* Empty string. */
  317.     check(strrchr(one, '\0') == one, 8);    /* NUL in empty string. */
  318.  
  319.     /*
  320.      * rindex - just like strrchr
  321.      */
  322.     it = "rindex";
  323.     check(rindex("abcd", 'z') == NULL, 1);    /* Not found. */
  324.     (void) strcpy(one, "abcd");
  325.     check(rindex(one, 'c') == one+2, 2);    /* Basic test. */
  326.     check(rindex(one, 'd') == one+3, 3);    /* End of string. */
  327.     check(rindex(one, 'a') == one, 4);    /* Beginning. */
  328.     check(rindex(one, '\0') == one+4, 5);    /* Finding NUL. */
  329.     (void) strcpy(one, "ababa");
  330.     check(rindex(one, 'b') == one+3, 6);    /* Finding last. */
  331.     (void) strcpy(one, "");
  332.     check(rindex(one, 'b') == NULL, 7);    /* Empty string. */
  333.     check(rindex(one, '\0') == one, 8);    /* NUL in empty string. */
  334. }
  335.  
  336. second()
  337. {
  338.     /*
  339.      * strpbrk - somewhat like strchr
  340.      */
  341.     it = "strpbrk";
  342.     check(strpbrk("abcd", "z") == NULL, 1);    /* Not found. */
  343.     (void) strcpy(one, "abcd");
  344.     check(strpbrk(one, "c") == one+2, 2);    /* Basic test. */
  345.     check(strpbrk(one, "d") == one+3, 3);    /* End of string. */
  346.     check(strpbrk(one, "a") == one, 4);    /* Beginning. */
  347.     check(strpbrk(one, "") == NULL, 5);    /* Empty search list. */
  348.     check(strpbrk(one, "cb") == one+1, 6);    /* Multiple search. */
  349.     (void) strcpy(one, "abcabdea");
  350.     check(strpbrk(one, "b") == one+1, 7);    /* Finding first. */
  351.     check(strpbrk(one, "cb") == one+1, 8);    /* With multiple search. */
  352.     check(strpbrk(one, "db") == one+1, 9);    /* Another variant. */
  353.     (void) strcpy(one, "");
  354.     check(strpbrk(one, "bc") == NULL, 10);    /* Empty string. */
  355.     check(strpbrk(one, "") == NULL, 11);    /* Both strings empty. */
  356.  
  357. #if 0
  358.     /*
  359.      * strstr - somewhat like strchr
  360.      */
  361.     it = "strstr";
  362.     check(strstr("abcd", "z") == NULL, 1);    /* Not found. */
  363.     check(strstr("abcd", "abx") == NULL, 2);    /* Dead end. */
  364.     (void) strcpy(one, "abcd");
  365.     check(strstr(one, "c") == one+2, 3);    /* Basic test. */
  366.     check(strstr(one, "bc") == one+1, 4);    /* Multichar. */
  367.     check(strstr(one, "d") == one+3, 5);    /* End of string. */
  368.     check(strstr(one, "cd") == one+2, 6);    /* Tail of string. */
  369.     check(strstr(one, "abc") == one, 7);    /* Beginning. */
  370.     check(strstr(one, "abcd") == one, 8);    /* Exact match. */
  371.     check(strstr(one, "abcde") == NULL, 9);    /* Too long. */
  372.     check(strstr(one, "de") == NULL, 10);    /* Past end. */
  373.     check(strstr(one, "") == one+4, 11);    /* Finding empty. */
  374.     (void) strcpy(one, "ababa");
  375.     check(strstr(one, "ba") == one+1, 12);    /* Finding first. */
  376.     (void) strcpy(one, "");
  377.     check(strstr(one, "b") == NULL, 13);    /* Empty string. */
  378.     check(strstr(one, "") == one, 14);    /* Empty in empty string. */
  379.     (void) strcpy(one, "bcbca");
  380.     check(strstr(one, "bca") == one+2, 15);    /* False start. */
  381.     (void) strcpy(one, "bbbcabbca");
  382.     check(strstr(one, "bbca") == one+1, 16);    /* With overlap. */
  383. #endif
  384.  
  385.     /*
  386.      * strspn
  387.      */
  388.     it = "strspn";
  389.     check(strspn("abcba", "abc") == 5, 1);    /* Whole string. */
  390.     check(strspn("abcba", "ab") == 2, 2);    /* Partial. */
  391.     check(strspn("abc", "qx") == 0, 3);    /* None. */
  392.     check(strspn("", "ab") == 0, 4);    /* Null string. */
  393.     check(strspn("abc", "") == 0, 5);    /* Null search list. */
  394.  
  395.     /*
  396.      * strcspn
  397.      */
  398.     it = "strcspn";
  399.     check(strcspn("abcba", "qx") == 5, 1);    /* Whole string. */
  400.     check(strcspn("abcba", "cx") == 2, 2);    /* Partial. */
  401.     check(strcspn("abc", "abc") == 0, 3);    /* None. */
  402.     check(strcspn("", "ab") == 0, 4);    /* Null string. */
  403.     check(strcspn("abc", "") == 3, 5);    /* Null search list. */
  404.  
  405.     /*
  406.      * strtok - the hard one
  407.      */
  408.     it = "strtok";
  409.     (void) strcpy(one, "first, second, third");
  410.     equal(strtok(one, ", "), "first", 1);    /* Basic test. */
  411.     equal(one, "first", 2);
  412.     equal(strtok((char *)NULL, ", "), "second", 3);
  413.     equal(strtok((char *)NULL, ", "), "third", 4);
  414.     check(strtok((char *)NULL, ", ") == NULL, 5);
  415.     (void) strcpy(one, ", first, ");
  416.     equal(strtok(one, ", "), "first", 6);    /* Extra delims, 1 tok. */
  417.     check(strtok((char *)NULL, ", ") == NULL, 7);
  418.     (void) strcpy(one, "1a, 1b; 2a, 2b");
  419.     equal(strtok(one, ", "), "1a", 8);    /* Changing delim lists. */
  420.     equal(strtok((char *)NULL, "; "), "1b", 9);
  421.     equal(strtok((char *)NULL, ", "), "2a", 10);
  422.     (void) strcpy(two, "x-y");
  423.     equal(strtok(two, "-"), "x", 11);    /* New string before done. */
  424.     equal(strtok((char *)NULL, "-"), "y", 12);
  425.     check(strtok((char *)NULL, "-") == NULL, 13);
  426.     (void) strcpy(one, "a,b, c,, ,d");
  427.     equal(strtok(one, ", "), "a", 14);    /* Different separators. */
  428.     equal(strtok((char *)NULL, ", "), "b", 15);
  429.     equal(strtok((char *)NULL, " ,"), "c", 16);    /* Permute list too. */
  430.     equal(strtok((char *)NULL, " ,"), "d", 17);
  431.     check(strtok((char *)NULL, ", ") == NULL, 18);
  432.     check(strtok((char *)NULL, ", ") == NULL, 19);    /* Persistence. */
  433.     (void) strcpy(one, ", ");
  434.     check(strtok(one, ", ") == NULL, 20);    /* No tokens. */
  435.     (void) strcpy(one, "");
  436.     check(strtok(one, ", ") == NULL, 21);    /* Empty string. */
  437.     (void) strcpy(one, "abc");
  438.     equal(strtok(one, ", "), "abc", 22);    /* No delimiters. */
  439.     check(strtok((char *)NULL, ", ") == NULL, 23);
  440.     (void) strcpy(one, "abc");
  441.     equal(strtok(one, ""), "abc", 24);    /* Empty delimiter list. */
  442.     check(strtok((char *)NULL, "") == NULL, 25);
  443.     (void) strcpy(one, "abcdefgh");
  444.     (void) strcpy(one, "a,b,c");
  445.     equal(strtok(one, ","), "a", 26);    /* Basics again... */
  446.     equal(strtok((char *)NULL, ","), "b", 27);
  447.     equal(strtok((char *)NULL, ","), "c", 28);
  448.     check(strtok((char *)NULL, ",") == NULL, 29);
  449.     equal(one+6, "gh", 30);            /* Stomped past end? */
  450.     equal(one, "a", 31);            /* Stomped old tokens? */
  451.     equal(one+2, "b", 32);
  452.     equal(one+4, "c", 33);
  453.  
  454.     /*
  455.      * memcmp
  456.      */
  457.     it = "memcmp";
  458.     check(memcmp("a", "a", 1) == 0, 1);    /* Identity. */
  459.     check(memcmp("abc", "abc", 3) == 0, 2);    /* Multicharacter. */
  460.     check(memcmp("abcd", "abce", 4) < 0, 3);    /* Honestly unequal. */
  461.     check(memcmp("abce", "abcd", 4) > 0, 4);
  462.     check(memcmp("alph", "beta", 4) < 0, 5);
  463.     if (charsigned)                /* Sign-bit comparison. */
  464.         check(memcmp("a\203", "a\003", 2) < 0, 6);
  465.     else
  466.         check(memcmp("a\203", "a\003", 2) > 0, 6);
  467.     check(memcmp("abce", "abcd", 3) == 0, 7);    /* Count limited. */
  468.     check(memcmp("abc", "def", 0) == 0, 8);    /* Zero count. */
  469.  
  470.     /*
  471.      * memchr
  472.      */
  473.     it = "memchr";
  474.     check(memchr("abcd", 'z', 4) == NULL, 1);    /* Not found. */
  475.     (void) strcpy(one, "abcd");
  476.     check(memchr(one, 'c', 4) == one+2, 2);    /* Basic test. */
  477.     check(memchr(one, 'd', 4) == one+3, 3);    /* End of string. */
  478.     check(memchr(one, 'a', 4) == one, 4);    /* Beginning. */
  479.     check(memchr(one, '\0', 5) == one+4, 5);    /* Finding NUL. */
  480.     (void) strcpy(one, "ababa");
  481.     check(memchr(one, 'b', 5) == one+1, 6);    /* Finding first. */
  482.     check(memchr(one, 'b', 0) == NULL, 7);    /* Zero count. */
  483.     check(memchr(one, 'a', 1) == one, 8);    /* Singleton case. */
  484.     (void) strcpy(one, "a\203b");
  485.     check(memchr(one, 0203, 3) == one+1, 9);    /* Unsignedness. */
  486.  
  487.     /*
  488.      * memcpy
  489.      *
  490.      * Note that X3J11 says memcpy must work regardless of overlap.
  491.      * The SVID says it might fail.
  492.      */
  493.     it = "memcpy";
  494.     check(memcpy(one, "abc", 4) == one, 1);    /* Returned value. */
  495.     equal(one, "abc", 2);            /* Did the copy go right? */
  496.  
  497.     (void) strcpy(one, "abcdefgh");
  498.     (void) memcpy(one+1, "xyz", 2);
  499.     equal(one, "axydefgh", 3);        /* Basic test. */
  500.  
  501.     (void) strcpy(one, "abc");
  502.     (void) memcpy(one, "xyz", 0);
  503.     equal(one, "abc", 4);            /* Zero-length copy. */
  504.  
  505.     (void) strcpy(one, "hi there");
  506.     (void) strcpy(two, "foo");
  507.     (void) memcpy(two, one, 9);
  508.     equal(two, "hi there", 5);        /* Just paranoia. */
  509.     equal(one, "hi there", 6);        /* Stomped on source? */
  510.  
  511.     (void) strcpy(one, "abcdefgh");
  512.     (void) memcpy(one+1, one, 9);
  513.     equal(one, "aabcdefgh", 7);        /* Overlap, right-to-left. */
  514.  
  515.     (void) strcpy(one, "abcdefgh");
  516.     (void) memcpy(one+1, one+2, 7);
  517.     equal(one, "acdefgh", 8);        /* Overlap, left-to-right. */
  518.  
  519.     (void) strcpy(one, "abcdefgh");
  520.     (void) memcpy(one, one, 9);
  521.     equal(one, "abcdefgh", 9);        /* 100% overlap. */
  522.  
  523.     /*
  524.      * memccpy - first test like memcpy, then the search part
  525.      *
  526.      * The SVID, the only place where memccpy is mentioned, says
  527.      * overlap might fail, so we don't try it.  Besides, it's hard
  528.      * to see the rationale for a non-left-to-right memccpy.
  529.      */
  530.     it = "memccpy";
  531.     check(memccpy(one, "abc", 'q', 4) == NULL, 1);    /* Returned value. */
  532.     equal(one, "abc", 2);            /* Did the copy go right? */
  533.  
  534.     (void) strcpy(one, "abcdefgh");
  535.     (void) memccpy(one+1, "xyz", 'q', 2);
  536.     equal(one, "axydefgh", 3);        /* Basic test. */
  537.  
  538.     (void) strcpy(one, "abc");
  539.     (void) memccpy(one, "xyz", 'q', 0);
  540.     equal(one, "abc", 4);            /* Zero-length copy. */
  541.  
  542.     (void) strcpy(one, "hi there");
  543.     (void) strcpy(two, "foo");
  544.     (void) memccpy(two, one, 'q', 9);
  545.     equal(two, "hi there", 5);        /* Just paranoia. */
  546.     equal(one, "hi there", 6);        /* Stomped on source? */
  547.  
  548.     (void) strcpy(one, "abcdefgh");
  549.     (void) strcpy(two, "horsefeathers");
  550.     check(memccpy(two, one, 'f', 9) == two+6, 7);    /* Returned value. */
  551.     equal(one, "abcdefgh", 8);        /* Source intact? */
  552.     equal(two, "abcdefeathers", 9);        /* Copy correct? */
  553.  
  554.     (void) strcpy(one, "abcd");
  555.     (void) strcpy(two, "bumblebee");
  556.     check(memccpy(two, one, 'a', 4) == two+1, 10);    /* First char. */
  557.     equal(two, "aumblebee", 11);
  558.     check(memccpy(two, one, 'd', 4) == two+4, 12);    /* Last char. */
  559.     equal(two, "abcdlebee", 13);
  560.     (void) strcpy(one, "xyz");
  561.     check(memccpy(two, one, 'x', 1) == two+1, 14);    /* Singleton. */
  562.     equal(two, "xbcdlebee", 15);
  563.  
  564.     /*
  565.      * memset
  566.      */
  567.     it = "memset";
  568.     (void) strcpy(one, "abcdefgh");
  569.     check(memset(one+1, 'x', 3) == one+1, 1);    /* Return value. */
  570.     equal(one, "axxxefgh", 2);        /* Basic test. */
  571.  
  572.     (void) memset(one+2, 'y', 0);
  573.     equal(one, "axxxefgh", 3);        /* Zero-length set. */
  574.  
  575.     (void) memset(one+5, 0, 1);
  576.     equal(one, "axxxe", 4);            /* Zero fill. */
  577.     equal(one+6, "gh", 5);            /* And the leftover. */
  578.  
  579.     (void) memset(one+2, 010045, 1);
  580.     equal(one, "ax\045xe", 6);        /* Unsigned char convert. */
  581.  
  582.     /*
  583.      * bcopy - much like memcpy
  584.      *
  585.      * Berklix manual is silent about overlap, so don't test it.
  586.      */
  587.     it = "bcopy";
  588.     (void) bcopy("abc", one, 4);
  589.     equal(one, "abc", 1);            /* Simple copy. */
  590.  
  591.     (void) strcpy(one, "abcdefgh");
  592.     (void) bcopy("xyz", one+1, 2);
  593.     equal(one, "axydefgh", 2);        /* Basic test. */
  594.  
  595.     (void) strcpy(one, "abc");
  596.     (void) bcopy("xyz", one, 0);
  597.     equal(one, "abc", 3);            /* Zero-length copy. */
  598.  
  599.     (void) strcpy(one, "hi there");
  600.     (void) strcpy(two, "foo");
  601.     (void) bcopy(one, two, 9);
  602.     equal(two, "hi there", 4);        /* Just paranoia. */
  603.     equal(one, "hi there", 5);        /* Stomped on source? */
  604.  
  605.     /*
  606.      * bzero
  607.      */
  608.     it = "bzero";
  609.     (void) strcpy(one, "abcdef");
  610.     bzero(one+2, 2);
  611.     equal(one, "ab", 1);            /* Basic test. */
  612.     equal(one+3, "", 2);
  613.     equal(one+4, "ef", 3);
  614.  
  615.     (void) strcpy(one, "abcdef");
  616.     bzero(one+2, 0);
  617.     equal(one, "abcdef", 4);        /* Zero-length copy. */
  618.  
  619.     /*
  620.      * bcmp - somewhat like memcmp
  621.      */
  622.     it = "bcmp";
  623.     check(bcmp("a", "a", 1) == 0, 1);    /* Identity. */
  624.     check(bcmp("abc", "abc", 3) == 0, 2);    /* Multicharacter. */
  625.     check(bcmp("abcd", "abce", 4) != 0, 3);    /* Honestly unequal. */
  626.     check(bcmp("abce", "abcd", 4) != 0, 4);
  627.     check(bcmp("alph", "beta", 4) != 0, 5);
  628.     check(bcmp("abce", "abcd", 3) == 0, 6);    /* Count limited. */
  629.     check(bcmp("abc", "def", 0) == 0, 8);    /* Zero count. */
  630.  
  631. #ifdef ERR
  632.     /*
  633.      * strerror - VERY system-dependent
  634.      */
  635.     it = "strerror";
  636.     f = open("/", 1);    /* Should always fail. */
  637.     check(f < 0 && errno > 0 && errno < sys_nerr, 1);
  638.     equal(strerror(errno), sys_errlist[errno], 2);
  639. #ifdef UNIXERR
  640.     equal(strerror(errno), "Is a directory", 3);
  641. #endif
  642. #ifdef BERKERR
  643.     equal(strerror(errno), "Permission denied", 3);
  644. #endif
  645. #endif
  646. }
  647.